$(function(){ 
	$("#idCard").on("blur",initArchives);
	$("#birthday").on("click",initArchives);
	$("body").height(document.documentElement.clientHeight);
    var picker = new mui.PopPicker();
	picker.setData([{
	    value: "1",
	    text: "自己"
	}, {
	    value: "2",
	    text: "父母"
	}, {
	    value: "3",
	    text: "爱人"
	}, {
	    value: "4",
	    text: "子女"
	}, {
	    value: "5",
	    text: "其他"
	}])
	$("#forme").click(function(){
		picker.show(function(item) {
			$("#forme").text(item[0].text).css("color","#333");
			$("input[name='ourself']").val(item[0].value);
		})
	});

	function initArchives(){
		//身份证号码
		var idcard = $("#idCard").val();
		if(idcard!=""){
			var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
			if(reg.test(idcard) === false){  
	       		mui.alert("身份证输入有误","提示");  
	       		return;  
   			}
			var Birthday;
			if(idcard.length==15){
				 Birthday = "19"+idcard.substr(6,6);  
			}else if(idcard.length==18){
				Birthday = idcard.substr(6,8); 
			}
			Birthday = Birthday.replace(/(.{4})(.{2})/,"$1-$2-");
			$("#birthday").text(Birthday).css("color","#333");
			$("input[name='birthday']").val(Birthday);
		}else{
			var today = new Date();
			var dtpicker = new mui.DtPicker({
			    type: "date",//设置日历初始视图模式 
			    beginDate: new Date(1900, 01, 01),//设置开始日期 
			    endDate: new Date(today.getFullYear(),today.getMonth(),today.getDate()-1),//设置结束日期 
			});
			$("#birthday").click(function(){
			    dtpicker.show(function(items) {
			    	$("#birthday").text(items.value).css("color","#333");
			    	$("input[name='birthday']").val(items.value);
				})  
			});
		}
		
	}
	
	//选择性别
	var sex=$("input[name='sex']:checked").val();
	
    $("#submit").on("click",function(){
    	//与你的关系
	    var me=$("#forme").text();
		if(me=="请选择"){
			mui.alert("请选择与您的关系!","提示");
			return;
		}
		//真实姓名
	    var name=$("#archivename").val();
		if(name.trim()==""){
			mui.alert("请输写名字!","提示");
			return;
		}
		//身份证号码
		var idcard = $("#idCard").val();
		//出生日期
		var birthday=$("#birthday").text();
		 
		if(birthday=="请选择"){
			mui.alert("请选择出生日期!","提示");
			return;	
		}
		//输入手机号
		var phone=$("#phone").val();
		if(phone.trim()==""){
			mui.alert("请输入手机号码!","提示");
			return;
		}
	    if(!(/^1[3456789]\d{9}$/.test(phone))){ 
	    	mui.alert("您的手机号码有误,请重新输入","提示");  
	        return; 
	    }
	  	var param =$('#thisform').serialize()+"&patientid="+USERID;
	    archivesModule.saveArchives(param,callBack);
	    function callBack(data){
			if(data.status==200||data ==1){
				mui.alert("保存成功","提示",function(){
					location.href="user-archives-list.html";
				}); 
			}else{
				mui.toast("保存失败");
			}
		}
    });
});





